package com.uzai.console.service.wechatprivate.impl;

import com.uzai.common.enums.GroupSendStatusEnum;
import com.uzai.console.dto.wechatprivate.devicewechatinfo.DeviceWechatInfoQuery;
import com.uzai.console.dto.wechatprivate.wxcontacts.*;
import com.uzai.console.entity.DeviceWechatInfo;
import com.uzai.console.entity.GroupSendJob;
import com.uzai.console.mapper.DeviceWechatInfoMapper;
import com.uzai.console.mapper.GroupSendJobMapper;
import com.uzai.console.service.wechatprivate.WxContactsService;
import com.uzai.console.service.wechatprivate.WxContactsSyncService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 个人微信-微信通讯录
 * @author liuqi
 * @date 2021年08月13日 11:34
 */
@Service
public class WxContactsSyncServiceImpl implements WxContactsSyncService {

    Logger logger = LoggerFactory.getLogger(WxContactsSyncServiceImpl.class);

    @Autowired
    private WxContactsService wxContactsService;
    @Autowired
    private DeviceWechatInfoMapper deviceWechatInfoMapper;
    @Autowired
    private GroupSendJobMapper groupSendJobMapper;

    /**
     * 查询结果添加群发任务
     * @author liuqi
     * @date 2021/8/25 11:38
     * @param addBatchSendMessageTaskDto
     * @return null
     */
    @Async("taskExecutor")
    @Override
    public void  sendMessageToUserSelectedSync(AddBatchSendMessageTaskDto addBatchSendMessageTaskDto){
        WxContactsQuery wxContactsQuery = addBatchSendMessageTaskDto.getWxContactsQuery();
        wxContactsQuery.setMerId(addBatchSendMessageTaskDto.getMerId());
        //查询条件中是否包含机器人id,
        if(wxContactsQuery.getDeviceUniqueId() != null ){//条件中带机器人id,则添加到机器人列表中
            wxContactsService.groupSendMsgByDeviceUniqueId(addBatchSendMessageTaskDto);
        }else{//如果没有，则查询该运营商所有的机器人
            DeviceWechatInfoQuery deviceWechatInfoQuery = new DeviceWechatInfoQuery();
            deviceWechatInfoQuery.setMerId(addBatchSendMessageTaskDto.getMerId());
            List<DeviceWechatInfo> deviceWechatInfoList = deviceWechatInfoMapper.findByList(deviceWechatInfoQuery);
            if(deviceWechatInfoList != null && deviceWechatInfoList.size() > 0){
                for(DeviceWechatInfo deviceWechatInfo : deviceWechatInfoList ){
                    try{
                        AddBatchSendMessageTaskDto addBatchSendMessageTaskDto_parent = new AddBatchSendMessageTaskDto();
                        addBatchSendMessageTaskDto_parent.setMerId(addBatchSendMessageTaskDto.getMerId());
                        addBatchSendMessageTaskDto_parent.setGroupSendJobParent(addBatchSendMessageTaskDto.getGroupSendJobParent());
                        addBatchSendMessageTaskDto_parent.setWechatUserGroupSendMsgDto(addBatchSendMessageTaskDto.getWechatUserGroupSendMsgDto());
                        WxContactsQuery wxContactsQuery_parent  = new WxContactsQuery();
                        BeanUtils.copyProperties(wxContactsQuery, wxContactsQuery_parent);
                        wxContactsQuery_parent.setDeviceUniqueId(deviceWechatInfo.getId());
                        addBatchSendMessageTaskDto_parent.setWxContactsQuery(wxContactsQuery_parent);
                        wxContactsService.groupSendMsgByDeviceUniqueId(addBatchSendMessageTaskDto_parent);
                    }catch (Exception e){
                        logger.error("-------sendMessageToUserSelectedSync is error; deviceUniqueId={}, error={}", wxContactsQuery.getDeviceUniqueId(), e.getMessage());
                    }
                }
            }
        }
    }

    /**
     * 新建群发消息(异步)
     * @author liuqi
     * @date 2021/8/25 11:38
     * @param newBatchSendMessageTaskDto
     * @return null
     */
    @Async("taskExecutor")
    @Override
    public void  newBatchSendMessageTaskSync(NewBatchSendMessageTaskDto newBatchSendMessageTaskDto, Long groupSendJobParentId, String loginName){

        //记录发送消息总数
        int sendMsgTotal_real_parent = 0;

        List<Long> deviceUniqueIdList = newBatchSendMessageTaskDto.getDeviceUniqueIdList();
        if(deviceUniqueIdList != null && deviceUniqueIdList.size() > 0){
            //循环机器人添加群发子任务
            for(Long deviceUniqueId :  deviceUniqueIdList){
                int sendMsgTotal_real_son = wxContactsService.newBatchSendMessageTaskSync(newBatchSendMessageTaskDto, groupSendJobParentId, deviceUniqueId, loginName);
                sendMsgTotal_real_parent = sendMsgTotal_real_parent + sendMsgTotal_real_son;
            }
        }

        if(groupSendJobParentId != null){
            GroupSendJob groupSendJob = groupSendJobMapper.selectById(groupSendJobParentId, newBatchSendMessageTaskDto.getMerId());
            if(groupSendJob != null){
                //修改主任务状态
                groupSendJob.setStatus(GroupSendStatusEnum.NOT_START.getValue());
                groupSendJob.setTotalCount(sendMsgTotal_real_parent);
                groupSendJobMapper.updateTotalCountById(groupSendJob);

                //批量修改子任务的状态
                groupSendJobMapper.updateStatusByParentId(GroupSendStatusEnum.NOT_START.getValue(), groupSendJobParentId, newBatchSendMessageTaskDto.getMerId());
            }
        }
    }

}
